<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="字段名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入字段名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userIds">
              <j-select-user-by-dep v-model="model.userIds" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="字段说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-textarea :rows="4" v-model="model.description" placeholder="请输入字段说明"  ></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="公式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="formulaId">
              <a-textarea :rows="10" v-model="model.formulaId" placeholder="请输入公式"  @click="()=>formulaMmodal.visible=true" ></a-textarea>
              <j-modal
                :visible.sync="formulaMmodal.visible"
                :width="600"
                :title="formulaMmodal.title"
              >
                <template>
                  <a-select  @change="onChange"   allowClear>
                    <a-select-option
                      v-for="(item,index) in formulaMmodalValues"
                      :key="index"
                      :value="item.value">
                      {{ item.text }}
                    </a-select-option>
                  </a-select>
                </template>
              </j-modal>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>
<script>


import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'WokerFieldForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        formulaMmodalValues: [
          {value:1,text:"if ( #迟到时长#(所有类型) >= 10 and #迟到时长#(所有类型) <= 30) { return 50;}\n" +
          "\n" + "if ( #迟到时长#(所有类型) >30) { return 100;}\n" +
          "\n" + "else return 0;"},
          {value:2,text:
          "if ( #班次名称# ==“夜班”） { return 30; } \n" +
          "\n" + " return 0 ;"},
          {value:3,text:
          "if ( #排了班# and #时间比较#(最晚打卡时间,大于等于,\"20:00\")) {return 30 ; } \n" +
          "\n" +" else  return 0 ;"},
          {value:4, text:
              "#加班时长#(小时,工作日,全部)*1.5+#加班时长#(小时,休息日,全部)*2+#加班时长#(小时,节假日,全部)*3;"}
        ],
        formulaMmodal: {
          title: "自定义公式",
          visible: false,
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: [
            { required: true, message: '请选择字段名称!'},
          ],
          userIds: [
            { required: true, message: '请选择负责人!'},
          ],
          // formulaId: [
          //   { required: true, message: '请输入公式!'},
          // ],
        },
        url: {
          add: "/satff/wokerField/add",
          edit: "/satff/wokerField/edit",
          queryById: "/satff/wokerField/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      selectShow() {

      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>